//
//  VisitingViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/28.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "VisitingViewController.h"

#import "VisitingDetailViewController.h"
#import "VisitingCell.h"
#import "TodoHttpManager.h"
#import "NewGenJinVC.h"
#import "MJRefresh.h"

@interface VisitingViewController ()<UITableViewDelegate,UITableViewDataSource,VisitingCellDelegate>

{
    UITableView *_tabView;
    NSMutableArray *_visitArr;
    NSMutableDictionary *_allDic;
    BOOL isPush;
    NSString *uid;
    NSString *nameStr;
}

@property(nonatomic,strong)UISearchBar * searchbar;
@property(nonatomic,strong)UITableView *tabView;
@property (nonatomic, strong) UIWebView *phoneWebView;
@property (nonatomic, strong) UIView *bgView;
@end

@implementation VisitingViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    if (isPush)
//    {
//        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuKeLaiF:) name:TDGuKeLaiF object:nil];
//        NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
//        
//        uid= [userDefa objectForKey:@"USER_ID"];
//        
//        //TODO:来访客户
//        [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:nil withJiao:nil withSeacher:nil withPaiXu:nil];
//    }
//    else
//    {
//        
//    }


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuKeLaiF:) name:TDGuKeLaiF object:nil];
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    uid= [userDefa objectForKey:@"USER_ID"];
    
    //TODO:来访客户
    [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:nil withJiao:nil withSeacher:nil withPaiXu:nil];


   
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    isPush = NO;
}
-(void)didTDGuKeLaiF:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _allDic = [notifi.object objectForKey:@"data"];
            _visitArr = [_allDic objectForKey:@"data"];
            //NSLog(@"11111-----%@",_allDic);
            
            
            
            [_tabView reloadData];
        }
        else
        {
           // [_manager MakeMyselfAlert:@"获取失败" andIsNeedTitle:NO andShowTime:2.0];
        }
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuKeLaiF object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Visi" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"VisiClear" object:nil];
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"VisitLouDou" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    isPush = YES;
    _isCall = NO;
    _manager = [TodoHttpManager sharedManager];
    _visitArr = [[NSMutableArray alloc] init];
    _allDic = [[NSMutableDictionary alloc] init];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT-45-44-LL_TabbarHeight-LL_StatusBarAndNavigationBarHeight) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.backgroundColor = BgColor;
    _tabView.rowHeight = 70;
     _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tabView];
    
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self selector:@selector(visitNotification:) name:@"Visi" object:nil];
    [notiCenter addObserver:self selector:@selector(visitClearNotification:) name:@"VisiClear" object:nil];
    //通知中心是个单例
 
    [notiCenter addObserver:self selector:@selector(visitLouNotification:) name:@"VisitLouDou" object:nil];
    
    

    //下拉刷新
    self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopic)];

    
    
}




#pragma mark--搜索框
- (void)visitNotification:(NSNotification *)noti
{
    
    // NSNotification 有三个属性，name, object, userInfo，其中最关键的object就是从第三个界面传来的数据。name就是通知事件的名字， userInfo一般是事件的信息。

    nameStr = noti.object;
    //TODO:来访客户
    [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:nil withJiao:nil withSeacher:nameStr withPaiXu:nil];
    [_tabView reloadData];
}

- (void)visitClearNotification:(NSNotification *)nf {
    nameStr = @"";
}

-(void)loadNewTopic
{
    if (nameStr.length==0)
    {
        //TODO:来访客户
        [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:nil withJiao:nil withSeacher:nil withPaiXu:nil];
        [_tabView reloadData];
    }
    else
    {
        //TODO:来访客户
        [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:nil withJiao:nil withSeacher:nameStr withPaiXu:nil];
        [_tabView reloadData];
    }
    
    [self.tabView.mj_header endRefreshing];
}

#pragma mark--漏斗搜索
-(void)visitLouNotification:(NSNotification *)notifi
{
    
    NSString *oneStr = [notifi.object objectForKey:@"OO"];
    NSString *twoStr = [notifi.object objectForKey:@"PP"];
    NSString *threeStr = [notifi.object objectForKey:@"QQ"];

     [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:oneStr withJiao:twoStr withSeacher:nil withPaiXu:threeStr];
    [_tabView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    if (searchText.length == 0)
    {
    [searchBar resignFirstResponder];
        [_manager UserGMKeLaiFangWithUserID:uid withKeLevel:nil withJiao:nil withSeacher:searchText withPaiXu:nil];
        //NSLog(@"=======%@",nameStr);

[_tabView reloadData];

    }

}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndent = @"Cell";
    VisitingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
    if (!cell)
    {
        cell = [[VisitingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
    }
    cell.delegate = self;
    cell.indexPath = indexPath;
    if ([cell isKindOfClass:[VisitingCell class]])
    {
        if (_visitArr.count != 0)
        {
            NSDictionary *dict = [_visitArr objectAtIndex:indexPath.row];
            
            [(VisitingCell *)cell setUpVisitCellWithImg:nil withName:dict[@"name"] withSex:dict[@"sex"] withFen:dict[@"khdj"] withFirstVisit:dict[@"time"] withGen:dict[@"genjin_time"] withBei:dict[@"genjin_data"] withPhone:dict[@"phone"]withChengJiaoState:dict[@"cjzt"]withCallStr:nil ];
            
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消选择效果
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewGenJinVC *newVC = [[NewGenJinVC alloc] init];
    newVC.genID = [[_visitArr objectAtIndex:indexPath.row] objectForKey:@"id"];
    newVC.ORCall = -_isCall;
    [self.navigationController pushViewController:newVC animated:NO];
//    VisitingDetailViewController *visitDetailVC = [[VisitingDetailViewController alloc] init];
//    visitDetailVC.visitID = [[_visitArr objectAtIndex:indexPath.row] objectForKey:@"id"];
//    [self.navigationController pushViewController:visitDetailVC animated:NO];
}

//.对于每个section返回一个cell

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _visitArr.count;
}

#pragma mark -- VisitingCellDelegate
- (void)didSelectPhone:(NSIndexPath *)indexPath {
    
    NSDictionary *dict = [_visitArr objectAtIndex:indexPath.row];
    NSString *phone = [dict[@"phone"] stringByReplacingOccurrencesOfString:@" " withString:@""];//去掉字符串中的空格
    NSMutableString *phoneStr=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    [self.phoneWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:phoneStr]]];
    [self.view addSubview:self.phoneWebView];
}

- (UIWebView *)phoneWebView {
    if (!_phoneWebView) {
        _phoneWebView = [[UIWebView alloc] init];
    }
    return _phoneWebView;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.view.bounds];
        _bgView.backgroundColor = [UIColor redColor];
        _bgView.userInteractionEnabled = NO;
    }
    return _bgView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
